from scipy.stats import percentileofscore
class fenweishu1():
    def __init__(self,timeperiod=1):
        self.timeperiod=timeperiod
        self.data=[]
        self.timekey = None
    def oncalc(self,d,timekey=None):
        rst=None
        if d!=None:
            if timekey != None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>self.timeperiod:
                self.data.pop(0)
            if len(self.data)==self.timeperiod:
                rst=percentileofscore(self.data, self.data[-1])
        return rst
class fenweishu2():
    def __init__(self,timeperiod=60,mintimeperiod=10):
        self.timeperiod=timeperiod
        self.mintimeperiod = mintimeperiod
        self.data=[]
        self.timekey = None
    def oncalc(self,d,timekey=None):
        rst=None
        if d!=None:
            if timekey != None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>self.timeperiod:
                self.data.pop(0)
            if len(self.data)>=self.mintimeperiod:
                rst=percentileofscore(self.data, self.data[-1])
        return rst